class_name EagleReturnState
extends GameplayState

@onready var eagle: Eagle = $"../../"

var _original_position: Vector2


func _on_enter() -> void:
    eagle.play_animation(&"idle")

    eagle.add_tag(GameplayTagLibrary.State_Air)

    var state_machine = get_parent() as GameplayStateMachine
    if state_machine:
        _original_position = state_machine.blackboard["original_position"] as Vector2


func on_update(delta: float) -> void:
    var return_direction = (_original_position - eagle.global_position).normalized()
    eagle.velocity = return_direction * eagle.get_attribute("return_speed").current_value * delta

    if eagle.global_position.distance_squared_to(_original_position) < 1.0:
        change_state("GhostState", func(): pass)
        return


func _on_exit() -> void:
    eagle.remove_tag(GameplayTagLibrary.State_Air)
